package jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbc {
    /**
     * 链接数据库
     * @return
     */
    public Connection lianjie() {
        Properties par = new Properties();
        String configFile = "dataBase.properties";
        String url = null;
        String username = null;
        String pwd = null;
        Connection con = null;
        try {
            //这里的类名指本类
            InputStream is = jdbc.class.getClassLoader().getResourceAsStream(configFile);
            par.load(is);
            String driver = par.getProperty("driver");
            url = par.getProperty("url");
            username = par.getProperty("user");
            pwd = par.getProperty("password");

            //加载驱动
            Class.forName(driver);
            //创建数据库链接管道
            con = DriverManager.getConnection(url, username, pwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭方法
     * @param res
     * @param sta
     * @param con
     * @throws SQLException
     */
    public void guanbi(ResultSet res, Statement sta, Connection con) throws SQLException {
        if(res!=null){
            res.close();
        }
        if(sta!=null){
            sta.close();
        }
        if(con!=null){
            con.close();
        }

    }
}
